{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第一章 Python基础"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 基础语法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**标识符**\n",
    "\n",
    "第一个字符必须是字母表中字母或下划线 _ 。\n",
    "标识符的其他的部分由字母、数字和下划线组成。\n",
    "标识符对大小写敏感。\n",
    "在 Python 3 中，可以用中文作为变量名，非 ASCII 标识符也是允许的了。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**python保留字**\n",
    "\n",
    "保留字即关键字，我们不能把它们用作任何标识符名称。Python 的标准库提供了一个 keyword 模块，可以输出当前版本的所有关键字："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# import keyword模块\n",
    "import keyword\n",
    "\n",
    "print(keyword.kwlist)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**注释**\n",
    "\n",
    "单行注释用 # ，多行注释可以用多个 # 号，还有 ''' 和 \"\"\"："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/python3\n",
    "\n",
    "# 这一行被称为shebang（也称为hashbang），它用于指定脚本的解释器路径。\n",
    "# 在Unix/Linux系统中运行一个脚本时，系统会读取这个shebang行，并使用指定的解释器来执行脚本。\n",
    "# 注意，并不是必须的，如果在终端使用python3命令来运行脚本，那么系统会自动使用python3解释器来执行脚本。\n",
    "# 比如：# python3 /path/to/script.py\n",
    "# 这种的就不需要 “#!/usr/bin/python3” 这一行了。\n",
    "# 但如果希望脚本能够直接在Unix/Linux系统中运行，而不需要显式地调用Python解释器，那么shebang行就是必要的。\n",
    "# \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Python!\n"
     ]
    }
   ],
   "source": [
    "# 第一个注释\n",
    "# 第二个注释\n",
    " \n",
    "'''\n",
    "第三注释\n",
    "第四注释\n",
    "'''\n",
    " \n",
    "\"\"\"\n",
    "第五注释\n",
    "第六注释\n",
    "\"\"\"\n",
    "print (\"Hello, Python!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**行与缩进**\n",
    "\n",
    "python最具特色的就是使用缩进来表示代码块，不需要像C语言使用大括号 {} 。  \n",
    "缩进的空格数是可变的，但是同一个代码块的语句必须包含相同的缩进空格数。实例如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "if True:\n",
    "    print (\"True\")\n",
    "else:\n",
    "    print (\"False\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面的代码中，最后一行代码语句的空格数不一致，导致运行报错："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unindent does not match any outer indentation level (<tokenize>, line 6)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m<tokenize>:6\u001b[0;36m\u001b[0m\n\u001b[0;31m    print (\"False1\")\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unindent does not match any outer indentation level\n"
     ]
    }
   ],
   "source": [
    "if True:\n",
    "    print (\"True\")\n",
    "    print (\"True1\")\n",
    "else:\n",
    "    print (\"False\")\n",
    "  print (\"False1\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意：“8 个空格”与“一个宽度相当于 8 个字符的制表符（Tab）”在外观上可能看起来相似，但从 Python 解释器的角度来说，它们并不一定等价。\n",
    "Python 通过字节流来区分空格和制表符字符，本质上它并不关心编辑器如何把 Tab 显示成几个空格。如果在同一代码块里混用了空格和 Tab，可能会导致 IndentationError 或者逻辑混乱。     \n",
    "Python 官方风格指南（PEP 8）强烈建议：使用 4 个空格来缩进，不要使用制表符。  \n",
    "示例如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "ename": "TabError",
     "evalue": "inconsistent use of tabs and spaces in indentation (388578281.py, line 6)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[19], line 6\u001b[0;36m\u001b[0m\n\u001b[0;31m    print (\"False1\")\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mTabError\u001b[0m\u001b[0;31m:\u001b[0m inconsistent use of tabs and spaces in indentation\n"
     ]
    }
   ],
   "source": [
    "if True:\n",
    "    print (\"True\")\n",
    "    print (\"True1\")\n",
    "else:\n",
    "        print (\"False\")\n",
    "\tprint (\"False1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unindent does not match any outer indentation level (<tokenize>, line 6)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m<tokenize>:6\u001b[0;36m\u001b[0m\n\u001b[0;31m    print (\"False1\")\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unindent does not match any outer indentation level\n"
     ]
    }
   ],
   "source": [
    "if True:\n",
    "\tprint (\"True\")\n",
    "\tprint (\"True1\")\n",
    "else:\n",
    "\tprint (\"False\")\n",
    "    print (\"False1\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "usr",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
